import tkinter as tk 
from tkinter import messagebox 
from config.Instrument import Instrument_toml  
  
class SwitchDatabaseWindow:  
    def __init__(self, master=None):  
        self.master = master  
        if not master:  
            self.master = tk.Tk()  
        self.master.title("切换数据库")  
        self.master.iconbitmap("static/favicon.ico")  
  
        # 设置窗口大小为400*200  
        width = self.master.winfo_screenwidth()  
        height = self.master.winfo_screenheight()  
        x = (width - 300) // 2  
        y = (height - 300) // 2  
        self.master.geometry("{}x{}+{}+{}".format(300, 300, x, y))  
  
        # 设置背景色  
        self.master.configure(bg="#fafafa")  
  
        self.user_label = tk.Label(self.master, text="用户")  
        self.user_label.configure(bg="#fafafa", fg="#3b80f9", font=("微软雅黑", 12))  
        self.user_label.place(x=40, y=40)  
  
        self.user_entry = tk.Entry(self.master)  
        self.user_entry.place(x=100, y=40)  
  
        self.address_label = tk.Label(self.master, text="地址")  
        self.address_label.configure(bg="#fafafa", fg="#3b80f9", font=("微软雅黑", 12))  
        self.address_label.place(x=40, y=80)  
        self.address_entry = tk.Entry(self.master)  
        self.address_entry.place(x=100, y=80)  
  
        self.password_label = tk.Label(self.master, text="密码")  
        self.password_label.configure(bg="#fafafa", fg="#3b80f9", font=("微软雅黑", 12))  
        self.password_label.place(x=40, y=120)  
        self.password_entry = tk.Entry(self.master, show="*")  
        self.password_entry.place(x=100, y=120)  
  
        self.dbname_label = tk.Label(self.master, text="数据库")  
        self.dbname_label.configure(bg="#fafafa", fg="#3b80f9", font=("微软雅黑", 12))  
        self.dbname_label.place(x=40, y=160)  
        self.dbname_entry = tk.Entry(self.master)  
        self.dbname_entry.place(x=100, y=160)  
  
          
  
        self.button = tk.Button(self.master, text="提交", command=self.ser_config)  
        self.button.configure(bg="#3b80f9", fg="#ffffff", font=("微软雅黑", 12), relief='flat', activeforeground='#3b80f9', activebackground='#fafafa')  
        self.button.place(x=120, y=200)  
      
    def ser_config(self):
        self.dic = {"url": self.address_entry.get(), "user": self.user_entry.get(), "password": self.password_entry.get(), "db": self.dbname_entry.get()}  
        toml = Instrument_toml()
        toml.SetStrogeToml("url",self.dic['url'])
        toml.SetStrogeToml("user",self.dic['user'])
        toml.SetStrogeToml("password",self.dic['password'])
        toml.SetStrogeToml("database",self.dic['db'])
        messagebox.showinfo("提示", "切换成功")
        self.master.destroy()
      
    def run(self):  
        self.master.mainloop()

